package com.bringway.p2p.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.domain.TreeNode;
import com.bringway.framework.domain.TreeNodes;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.service.MenuService;
import com.bringway.p2p.domain.Tree;
import com.bringway.p2p.service.P2PRoleService;
import net.sf.json.JSONObject;

/*
 * @author fan
 * @className P2PRoleController
 * @description P2P角色控制器
 * @date 2015年12月20日下午2:29:49
 */
@Controller
@RequestMapping("/role")
public class P2PRoleController extends JsonController {
	@Autowired
	private P2PRoleService p2pRoleService;
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * @方法名称:getRoleListAll
	 * @方法描述:获取所有的角色列表
	 * @author fan
	 * @date 2016年1月22日下午4:58:01
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryRoleListAll")
	public ModelAndView getRoleListAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list = p2pRoleService.getRoleList();
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @方法名称:getRoleList
	 * @方法描述:获取角色列表---根据父级角色
	 * @author fan
	 * @date 2015年12月20日下午2:31:40
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryRoleList")
	public ModelAndView getRoleList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String parent_id = RequestUtils.getStringParameter(request, "node");
		if (StringUtils.isEmpty(parent_id)) {
			parent_id="-1";
		}
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("parent_id", parent_id);
		List<Map<String, Object>> list = p2pRoleService.getRoleList(parms);
		ArrayList nodeList = new ArrayList();
		Iterator ite = list.iterator();
		while (ite.hasNext()) {
			Map map = (Map) ite.next();
			ite.remove();
			TreeNode n = new TreeNode();
			n.setAsync(true);
			n.setText(map.get("NAME").toString());
			n.setId(map.get("ID").toString());
			n.setParentId(map.get("PARENT_ID").toString());
			nodeList.add(n);
		}
		serialize(request, response, nodeList, true);
		return null;
	}
	
	
	
	@RequestMapping("/queryRoleListchecked")
	public ModelAndView getRoleListchecked(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String menuId = RequestUtils.getStringParameter(request, "menuId");//菜单左侧选择menuId
		String roleParentId = RequestUtils.getStringParameter(request, "nodeId");//角色列表父节点ID
		if (StringUtils.isEmpty(roleParentId)) {
			roleParentId="-1";
		}
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("menuId", menuId);//选择的菜单ID
		parms.put("parent_id", roleParentId);//下拉树的角色父节点ID
		List<Map<String, Object>> list = p2pRoleService.getRoleList(parms);
		// 定义一个list ，用来存储应该被选中的菜单的id
		List<Map<String, Object>> list2 = menuService.getRoleListByMenuId(parms);
		ArrayList nodeList = new ArrayList();
		Iterator ite = list.iterator();
		while (ite.hasNext()) {
			Map map = (Map) ite.next();
			ite.remove();
			TreeNodes n = new TreeNodes();
			/*// 如果list2中包含了这个id，那么就将这个checkbox选中
			if (list2.contains(map.get("ID").toString())) {
				n.setChecked(true);
			}*/
			for(Map<String,Object> roleMap : list2){
				String list1_roleId = MapUtils.getString(map, "ID");
				String list2_roleId = MapUtils.getString(roleMap, "ROLE_ID");
				if(list1_roleId.equals(list2_roleId)){
					n.setChecked(true);
					break;
				}
			}
			n.setAsync(true);
			n.setText(map.get("NAME").toString());
			n.setId(map.get("ID").toString());
			n.setParentId(map.get("PARENT_ID").toString());
			nodeList.add(n);
		}
		serialize(request, response, nodeList, true);
		return null;	
			
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * @方法名称:addRole
	 * @方法描述:新增角色
	 * @author fan
	 * @date 2015年12月21日下午2:33:40
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addRole")
	public ModelAndView addRole(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = UUID.randomUUID().toString();
		String name = RequestUtils.getStringParameter(request, "name");
		String parent_id = RequestUtils.getStringParameter(request, "parent_id");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("id", id);
		parms.put("name", name);
		parms.put("parent_id", parent_id);
		boolean bool = p2pRoleService.addRole(parms);
		result.put("success",true);
		result.put("isAddSuc", bool);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @方法名称:deleteRole
	 * @方法描述:删除角色
	 * @author fan
	 * @date 2015年12月21日下午3:08:24
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteRole")
	public ModelAndView deleteRole(HttpServletRequest request ,HttpServletResponse response) throws Exception{
		String id = RequestUtils.getStringParameter(request, "id");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("id", id);
		boolean bool = p2pRoleService.delRole(parms);
		result.put("success",true);
		result.put("isDel", bool);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @方法名称:updateRole
	 * @方法描述:修改角色的方法
	 * @author fan
	 * @date 2015年12月21日下午3:44:29
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/updateRole")
	public ModelAndView updateRole(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = RequestUtils.getStringParameter(request, "id");
		String name = RequestUtils.getStringParameter(request, "name");
		String parent_id = RequestUtils.getStringParameter(request, "parent_id");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("id", id);
		parms.put("name", name);
		parms.put("parent_id", parent_id);
		boolean bool = p2pRoleService.updateRole(parms);
		result.put("success",true);
		result.put("isUpdate", bool);
		serialize(request,response, result);
		return null;
	}
}
